<?php

namespace App\Model;

use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Db\Cursor;
use EasySwoole\ORM\Db\CursorInterface;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\Pool\Manager;
use Throwable;

/**
 * Class ActivityModel
 * Create With Automatic Generator
 * @property $id
 * @property $btype_id
 * @property $stype_id
 * @property $name
 * @property $start_date
 * @property $end_date
 * @property $start_time
 * @property $end_time
 * @property $expiration_datetime
 * @property $virtual_reward
 * @property $real_reward
 * @property $all_reward
 * @property $virtual_enrollment
 * @property $real_enrollment
 * @property $all_enrollment
 * @property $status
 * @property $days_held
 * @property $create_time
 * @property $update_time
 * @property $bet_amount
 * @property $is_add
 * @property $is_coupons
 * @property $award_time
 * @property $coupons_real_money
 * @property $coupons_virtual_money
 * @property $coupons_money
 * @property $game_type
 */
class ActivityModel extends AbstractModel
{
    protected $tableName = 'bee_activity';

    protected $primaryKey = 'id';

    protected $autoTimeStamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * 添加赛事
     * @param ActivityRuleModel $activityRuleModel
     * @return bool|int
     * @throws Exception
     * @throws Throwable
     */
    public function addActivity(ActivityRuleModel $activityRuleModel)
    {
        $res = $this->data(
            [
                'btype_id' => $activityRuleModel->btype_id,
                'stype_id' => $activityRuleModel->stype_id,
                'name' => date('Ymd', time() + 86400),
                'start_date' => date('Y-m-d', time() + 86400),
                'end_date' => date('Y-m-d', (time() + 86400) + (($activityRuleModel->days_held - 1) * 86400)),
                'start_time' => $activityRuleModel->start_time,
                'end_time' => $activityRuleModel->end_time,
                'expiration_datetime' => date('Y-m-d 23:59:59', time()),
                'virtual_reward' => 0.00,
                'real_reward' => 0.00,
                'all_reward' => 0.00,
                'virtual_enrollment' => 0,
                'real_enrollment' => 0,
                'all_enrollment' => 0,
                'status' => 1,
                'days_held' => $activityRuleModel->days_held,
                'create_time' => time(),
                'update_time' => time(),
                'bet_amount' => $activityRuleModel->bet_amount,
                'is_add' => $activityRuleModel->is_add,
                'is_coupons' => $activityRuleModel->is_coupons,
                'award_time' => $activityRuleModel->award_time,
                'coupons_real_money' => 0.00,
                'coupons_virtual_money' => 0.00,
                'coupons_money' => 0.00,
                'game_type' => $activityRuleModel->game_type,
            ], false)->save();

        if (is_numeric($res)) {
            $infoArray = $this->get($res)->toArray();
            unset($infoArray['create_time'], $infoArray['update_time']);
            go(function () use ($res, $infoArray) {
                // 插入真实的有序集合中
                $redis = Manager::getInstance()->get('redis')->getObj();
                $redis->hMSet('activity_' . $res, $infoArray);
                Manager::getInstance()->get('redis')->recycleObj($redis);
            });
        }

        return $res;
    }

    /**
     * 通过ID获取赛事信息
     * @param int $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function findActivity(int $id)
    {
        $keyName = 'activity_' . $id;
        $redis = Manager::getInstance()->get('redis')->getObj();
        if ($redis->exists($keyName)) {
            $infoArray = $redis->hValS($keyName);
        } else {
            $infoArray = $this->get($id)->toArray();
            unset($infoArray['create_time'], $infoArray['update_time']);
            $redis->hMSet($keyName, $infoArray);
        }
        Manager::getInstance()->get('redis')->recycleObj($redis);
        return $infoArray;
    }

    /**
     * 更新赛事
     * @param int $id
     * @param array $data
     * @return ActivityModel|array|bool|AbstractModel|Cursor|CursorInterface|null
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function updateActivity(int $id, array $data)
    {
        if (!count($data)) {
            return false;
        }

        $info = $this->get($id);
        foreach ($data as $k => $v) {
            if (is_array($v)) {
                $this->$k = QueryBuilder::$v[0]($v[1]);
            } else {
                $this->$k = $v;
            }
        }
        $info->update();

        go(function () use ($id, $data) {
            $redis = Manager::getInstance()->get('redis')->getObj();
            foreach ($data as $k => $v) {
                if (is_array($v)) {
                    if (is_int($v[1])) {
                        $redis->hIncrBy('activity_' . $id, $k, ($v[0] == 'inc' ? $v[1] : $v[1] * -1));
                    } elseif (is_float($v[1])) {
                        $redis->hIncrByFloat('activity_' . $id, $k, ($v[0] == 'inc' ? $v[1] : $v[1] * -1));
                    }
                } else {
                    $redis->hSet('activity_' . $id, $k, $v);
                }
            }
            Manager::getInstance()->get('redis')->recycleObj($redis);
        });

        return $info->toArray();
    }
}